<template>
    <div class="debris-list" v-loading="loading">
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">关联游戏</span>
            <el-select
                style="width:213px"
                v-model="gameName"
                clearable
                :loading="gameLoading"
                @change="selectChange"
                placeholder="请输入并选择游戏">
                <el-option
                    v-for="item in gameList"
                    :key="item.gameid"
                    :label="item.appName"
                    :value="item.appName">
                        <span style="float: left">
                            <span>{{ item.appName }}</span>
                            <span v-if="item.status === -1" style="color:red;font-size:12px;margin:0 5px;">下架</span>
                        </span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.gameid }}</span>
                </el-option>
            </el-select>

            <span class="input-title">商品状态</span>
            <el-select
                @change="selectChange"
                style="width:150px;"
                v-model="status">
                <el-option label="全部" :value="null"></el-option>
                <el-option label="可兑换" :value="1"></el-option>
                <el-option label="已下架" :value="0"></el-option>
            </el-select>

            <span class="input-title">碎片数</span>
            <el-input
                placeholder="请输入碎片数量"
                @keyup.enter.native="selectChange"
                style="width:170px;"
                clearable
                v-model.trim="piecePrice">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
        </div>
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">关键字搜索</span>
            <el-input
                placeholder="请输入英雄名/皮肤名"
                @keyup.enter.native="selectChange"
                style="width:200px;"
                clearable
                v-model.trim="keyWord">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <el-button type="primary" class="btn" style="margin-left:20px" @click="getList(1)">查询</el-button>
            <el-button type="success" class="btn" @click="goDebrisDetail(0)">新增商品</el-button>
        </div>

        <div class="table-box">
            <el-table
                :data="tableData"
                ref="table"
                @sort-change="sortChange"
                border
                style="width: 100%">
                <el-table-column fixed width="100" prop="skinId" label="商品ID"></el-table-column>
                <el-table-column width="100" prop="heroName" label="道具"></el-table-column>
                <el-table-column min-width="100" prop="gameName" label="道具描述">
                    <template slot-scope="scope">
                        <div>{{ scope.row.name }}</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="商品图鉴">
                    <template slot-scope="scope">
                        <imgBox :url="scope.row.thumbnail" :width="80"/>
                    </template>
                </el-table-column>
                <el-table-column label="商品类型">
                    <template slot-scope="">
                        <div>皮肤</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" label="关联游戏">
                    <template slot-scope="scope">
                        <span class="table-btn" @click="goGameDetail(scope.row.gameId)">{{ scope.row.gameName || '-' }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="碎片数">
                    <template slot-scope="scope">
                        <div>{{ scope.row.piecePrice || '-' }}</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="兑换量">
                   <template slot-scope="scope">
                        <div>{{ scope.row.userGetNumber || '0' }}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="商品状态">
                    <template slot-scope="scope">
                        <div style="color: #67C23A;" v-if="scope.row.status === 1">
                            <span class="point"></span>
                            <span>可兑换</span>
                        </div>
                        <div style="color:#F56C6C;" v-if="scope.row.status === 0">
                            <div class="point red-point"></div>
                            <span>已下架</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <div v-if="scope.row.auditstatus !== -1">
                            <el-button v-if="scope.row.status === 1" @click="changeStatus(scope.row, 1)" type="text" size="small" style="color:#F56C6C;">商品下架</el-button>
                            <el-button v-if="scope.row.status === 0" @click="changeStatus(scope.row, 0)" type="text" size="small" style="color:#67C23A;">商品上架</el-button>
                        </div>
                        <div>
                            <el-button @click="goExchangeDetail(scope.row)" type="text" size="small" style="color:#E6A23C;">用户兑换明细</el-button>
                        </div>
                        <div>
                            <el-button @click="goDebrisDetail(scope.row.skinId)" type="text" size="small">查看详情</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            gameName: null, // 查询的游戏
            piecePrice: null,
            keyWord: null,
            status: null,
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            gameLoading: false, // 查询游戏的loading
            gameList: [] // 游戏列表
        }
    },

    activated () {
        if (location.host.indexOf('work.duodian.cn') !== -1) {
            this.gameList = this._proGameList
        } else {
            this.gameList = this._devGameList
        }
        this.getList(this.pageNum)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            this.listParams = {
                gameName: this.gameName,
                status: this.status,
                piecePrice: this.piecePrice,
                keyWord: this.keyWord,
                pageNum: pageNum,
                pageSize: this.pageSize
            }

            let res = await this.$http.get('/lottrey/getPiece', {
                params: this.listParams
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        // 后端排序
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = ''
            } else {
                this.type = prop
            }
            this.getList(1)
        },
        selectChange () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },
        // 查询游戏
        filterMethod (res) {
            clearTimeout(this.timerId)
            if (!res) {
                this.gameLoading = false
                return
            }
            this.gameLoading = true
            this.timerId = setTimeout(() => {
                this.getgameList(res)
            }, 1000)
        },
        async getgameList (keyword) {
            let params = {
                keyword,
                pageIndex: 1,
                pageNum: 100
            }
            let res = await this.$http.post('/home/findGames', params)
            if (res.code === '0') {
                this.gameList = res.data
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.gameLoading = false
        },
        changeStatus (item, status) {
            this.$http.post('/lottrey/updateStatus', {
                id: item.skinId,
                status: status > 0 ? 0 : 1
            }).then(res => {
                if (res.code === '0') {
                    let message = status > 0 ? '下架成功' : '上架成功'
                    item.status = status > 0 ? 0 : 1
                    this.$message({
                        type: 'success',
                        message
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },
        goExchangeDetail (item) {
            this.$router.push({
                path: '/lottery/debrisUser',
                query: {
                    gameName: item.gameName,
                    name: item.name
                }
            })
        },

        // 查看游戏详情
        goGameDetail (id) {
            const { href } = this.$router.resolve({
                path: '/game/gameDetail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        // 查看碎片详情
        goDebrisDetail (id) {
            let query = {}
            if (id) {
                query.id = id
            }
            this.$router.push({
                path: '/lottery/debrisDetail',
                query
            })
        }
    },
    filters: {
        timeInit (val) {
            if (val) {
                return formatDate(val, 'YYYY-MM-DD HH:MM:SS')
            } else {
                return '至今'
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .debris-list {
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .desc {
                overflow: hidden;
                line-height: 20px;
                -webkit-line-clamp: 5;
                -webkit-box-orient: vertical;
                display: -webkit-box;
                height: 100px;
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
